package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Shipping;
import com.mmall.pojo.User;
import com.mmall.service.IShippingService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/shipping/")
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;

    /**
     * 收货地址添加
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping(value = "addShipping.do")
    @ResponseBody
    public ServerResponse addShipping(HttpSession session,Shipping shipping){

        //判断用户是否登录
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return  iShippingService.addShipping(user.getId(),shipping);
    }

    /**
     * 删除收货地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping(value = "deleteShipping.do")
    @ResponseBody
    public ServerResponse deleteShipping(HttpSession session,Integer shippingId){

        //判断用户是否登录
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return  iShippingService.deleteShipping(user.getId(),shippingId);
    }

    /**
     * 更新收货地址
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping(value = "updateShipping.do")
    @ResponseBody
    public ServerResponse updateShipping(HttpSession session,Shipping shipping){

        //判断用户是否登录
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return  iShippingService.updateShipping(user.getId(),shipping);
    }

    /**
     * 查看当前收货地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping(value = "selectShipping.do")
    @ResponseBody
    public ServerResponse selectShipping(HttpSession session,Integer shippingId){

        //判断用户是否登录
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return  iShippingService.selectShipping(user.getId(),shippingId);
    }


    /**
     * 查看所有收货地址
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "selectShippingAll.do")
    @ResponseBody
    public ServerResponse selectShippingAll(HttpSession session,Integer pageNum,Integer pageSize){

        //判断用户是否登录
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc() );
        }
        return  iShippingService.selectShippingAll(user.getId(),pageNum,pageSize);
    }
}
